#!/bin/sh
#
# Copyright (C) 2015, 2016, 2018, 2020, 2022  Etersoft
# Copyright (C) 2008, 2015, 2016, 2018, 2020, 2022  Vitaly Lipatov <lav@etersoft.ru>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

# TODO: improve file
is_pkgfile()
{
     [ -f "$1" ] || return
     echo "$1" | grep -q "\.rpm$" && return
     echo "$1" | grep -q "\.deb$" && return
     return 1
}

# Query variables from rpm package
# TODO: rpm only
rpm_query_package_format_field()
{
    local FORMAT="$1\n"
    shift
    local INSTALLED=""
    # if a file, add -p for get from rpm base
    if is_pkgfile "$1" ; then
        INSTALLED="-p"
    fi
    a= rpmquery $INSTALLED --queryformat "$FORMAT" "$@"
}

rpm_query_package_field()
{
    local FORMAT="%{$1}"
    shift
    rpm_query_package_format_field "$FORMAT" "$@"
}

dpkg_query_package_format_field()
{
        local field="$1"
        shift
        if is_pkgfile "$1" ; then
            a= dpkg-deb --show --showformat="$field\n" "$@"
        else
            #a= dpkg -s "$1" | grep "^$field: " | sed -e "s|^$field: ||"
            a= dpkg-query -W --showformat="$field\n" -- "$@"
        fi
}

dpkg_query_package_field()
{
        local field="$1"
        shift
        #if [ -f "$1" ] ; then
        #    a= dpkg -I "$@" | grep "^.*$field: " | sed -e "s|^.*$field: ||"
        #else
            dpkg_query_package_format_field "\${$field}" "$@"
        #fi
}

query_package_field()
{
    local field="$1"
    shift
    case $PMTYPE in
        *-dpkg)
            dpkg_query_package_field "$field" "$@"
            ;;
        *-rpm)
            rpm_query_package_field "$field" "$@"
            ;;
    esac
}


print_pkg_arch()
{
    case $PMTYPE in
        *-dpkg)
            dpkg_query_package_field "Arch" "$@" | sed -e "s|-.*||" -e "s|.*:||"
            ;;
        *-rpm)
            rpm_query_package_field "arch" "$@"
            ;;
    esac
}

print_pkg_version()
{
    case $PMTYPE in
        *-dpkg)
            dpkg_query_package_field "Version" "$@" | sed -e "s|-.*||" -e "s|.*:||"
            ;;
        *-rpm)
            rpm_query_package_field "version" "$@"
            ;;
    esac
}

print_pkg_release()
{
    case $PMTYPE in
        *-dpkg)
            dpkg_query_package_field "Version" "$@" | sed -e "s|.*-||"
            ;;
        *-rpm)
            rpm_query_package_field "release" "$@"
            ;;
    esac
}

print_pkg_version_release()
{
    case $PMTYPE in
        *-dpkg)
            dpkg_query_package_field "Version" "$@" | sed -e "s|.*:||"
            ;;
        *-rpm)
            rpm_query_package_format_field "%{version}-%{release}" "$@"
            ;;
    esac
}

print_pkg_name()
{
    case $PMTYPE in
        *-dpkg)
            dpkg_query_package_field "Package" "$@"
            ;;
        *-rpm)
            rpm_query_package_field "name" "$@"
            ;;
    esac
}

# build binary package list (1st - repo dir, 2st - pkgname)
# algorithm: list all files in PKGDIR, print packages with our source pkg name
print_binpkgfilelist()
{
    local PKGDIR=$1
    local PKGNAME=$(basename $2)
    find "$PKGDIR" ! -name '*\.src\.rpm' -name '*\.rpm' -execdir \
        rpmquery -p --qf='%{sourcerpm}\t%{name}-%{version}-%{release}.%{arch}.rpm\n' "{}" \; \
        | grep "^$PKGNAME[[:space:]].*" \
        | cut -f2 \
        | xargs -n1 -I "{}" echo -n "$PKGDIR/{} "
}

# TODO: need try detect more strict
# TODO: package name mask for every system
# TODO: broken for all systems
#PKGNAMEMASK1="\(.*\)-\([^0-9].*[^0-9]\)-\(.*[0-9].*\)"
# mask to parse package name

# TODO: с хвоста, сначала релиз, потом версию, остаётся пакет.
# всё равно остаются неоднозначности: libname-1.0 где 1.0 то ли версия, то ли часть названия.
# возможно, нужно ориентироваться только на выделение в полном имени/

# libpq5.2-9.0eter-9.0.4-alt14
# nx-libs-3.5.99.26.1-eter2astra:amd64
PKGNAMEMASK4="6\(.*\)[_-]\([^_-]*\)[_-]\(.*[0-9].*\):\(.*\)$"
# nx-libs-3.5.99.26.1-eter2astra
PKGNAMEMASK3="^\(.*\)[_-]\([^_-]*\)[_-]\(.*[0-9].*\)$"
# nx-libs-3.5.99.26.1
#PKGNAMEMASK2="^\(.*\)[_-]\([0-9].*\)$"

PKGNAMEMASK="\(.*\)-\([0-9].*\)-\(.*[0-9].*\)\.\(.*\)\.\(.*\)"

print_name()
{
    # FIXME:
    # don't change name (false cases)
    #echo "$@" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK4|\1-\2-\3|" -e "s|$PKGNAMEMASK3|\1|"
    echo "$@" | xargs -n1 echo
}

# as hack for print_name
print_shortname()
{
    #if [ "$
    #echo "$@" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK4|\1-\2-\3|" -e "s|$PKGNAMEMASK3|\1|"
    echo "$@" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK3|\1|"
}

print_version()
{
    echo "$1" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK4|\1-\2-\3|" -e "s|$PKGNAMEMASK3|\2|"
}

print_release()
{
    echo "$1" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK4|\1-\2-\3|" -e "s|$PKGNAMEMASK3|\3|"
}

print_version_release()
{
    echo "$1" | xargs -n1 echo | sed -e "s|$PKGNAMEMASK4|\1-\2-\3|" -e "s|$PKGNAMEMASK3|\2-\3|"
}

# get package name only by package filename
# TODO: see also __epm_get_hilevel_name()
print_pkgname()
{
    local i
    for i in $@ ; do
        # TODO: deb and other, arch string
        echo "$(basename "$i") " | sed -e "s|\.[a-z_0-9]*\.rpm||g" -e "s|\(.*\)_\(.*\)_[a-z_0-9]*\.deb|\1-\2|g"
    done
}

print_srcname()
{
    print_name "$(print_srcpkgname "$@")"
}

print_specname()
{
    # CHECKME: it is possible to have two or more specs in one package?
    a= rpm -qlp "$@" | grep "\.spec\$"
}

print_srcpkgname()
{

    if [ -n "$FNFLAG" ] ; then
        rpm_query_package_field "sourcerpm" "$@"
        return
    fi

    # if PKFLAG
    case $PMTYPE in
        apt-dpkg)
            fatal "Unknown command for get source package name via dpkg"
            ;;
        urpm-rpm)
            docmd urpmq --sourcerpm "$@"
            return
            ;;
        dnf-rpm)
            showcmd dnf repoquery --qf '%{SOURCERPM}' "$@"
            a= dnf repoquery --qf '%{SOURCERPM}' "$@"
            return
            ;;
    esac

    # FIXME: only for installed rpm packages
    rpm_query_package_field "sourcerpm" "$@"
}

# https://blog.jasonantman.com/2014/07/how-yum-and-rpm-compare-versions/
compare_version()
{
    case $PMTYPE in
        *-rpm)
            # rpmevrcmp exists in ALT Linux only
            if is_command rpmevrcmp ; then
                a= rpmevrcmp "$@"
            else
                a= rpm --eval "%{lua:print(rpm.vercmp('$1', '$2'))}"
            fi
            ;;
        *-dpkg)
            a= dpkg --compare-versions "$1" lt "$2" && echo "-1" && return
            a= dpkg --compare-versions "$1" eq "$2" && echo "0" && return
            echo "1"
            ;;
        *)
            fatal "Not implemented for $PMTYPE"
            ;;
    esac
}

# construct package file name.
# name version [arch] [pkgtype] [ds] [pds]
construct_name()
{
    local name="$1"
    local version="$2"
    local arch="$3"
    local pkgtype="$4"
    local ds="$5"
    local pds="$6"

    [ -n "$arch" ] || arch="$DISTRARCH"
    [ -n "$pkgtype" ] || pkgtype="$PKGFORMAT"
    [ -n "$ds" ] || ds=$(get_pkg_name_delimiter $pkgtype)
    [ -z "$pds" ] && pds="$ds" && [ "$pds" = "-" ] && pds="."
    [ -n "$version" ] && version="$ds$version"
    echo "${name}${version}${pds}$arch.$pkgtype"
}

epm_print_help()
{
cat <<EOF
  Examples:
    epm print info [args]                    print system and distro info (via distro_info command)
    epm print name [from filename|for package] NN        print only name of package name or package file
    epm print shortname [for package] NN        print only short name of package name
    epm print version [from filename|for package] NN     print only version of package name or package file
    epm print release [from filename|for package] NN     print only release of package name or package file
    epm print version-release [from filename|for package] NN     print only release-release of package name or package file
    epm print arch [from filename|for package] NN     print arch  of package name or package file
    epm print field FF for package NN        print field of the package
    epm print pkgname from filename NN       print package name for the package file
    epm print srcname from filename NN       print source name for the package file
    epm print srcpkgname from [filename|package] NN    print source package name for the binary package file
    epm print specname from filename NN      print spec filename for the source package file
    epm print binpkgfilelist in DIR for NN   list binary package(s) filename(s) from DIR for the source package file
    epm print compare [package] version N1 N2          compare (package) versions and print -1 (N1 < N2), 0 (N1 == N2), 1 (N1 > N2)
    epm print constructname <name> <version> [arch] [pkgtype] [delimiter1] [delimiter2]  print distro dependend package filename from args name version arch pkgtype
EOF
}

epm_print()
{
    local WHAT="$1"
    shift
    local FNFLAG=
    local PKFLAG=
    [ "$1" = "from" ] && shift
    [ "$1" = "for" ] && shift
    [ "$1" = "of" ] && shift
    [ "$1" = "in" ] && shift
    if [ "$1" = "filename" ] ; then
        FNFLAG="$1"
        shift
    fi

    if [ "$1" = "package" ] ; then
        PKFLAG="$1"
        shift
    fi

    case "$WHAT" in
        "")
            fatal "Use epm print --help to get help."
            ;;
        "-h"|"--help"|"help")
            epm_print_help
            ;;
        "name")
            [ -n "$1" ] || fatal "Arg is missed"
            if [ -n "$FNFLAG" ] ; then
                print_name "$(print_pkgname "$@")"
            elif [ -n "$PKFLAG" ] ; then
                print_pkg_name "$@"
            else
                print_name "$@"
            fi
            ;;
        "arch")
            [ -n "$1" ] || fatal "Arg is missed"
            if [ -n "$FNFLAG" ] ; then
                print_pkg_arch "$@"
            elif [ -n "$PKFLAG" ] ; then
                print_pkg_arch "$@"
            else
                print_pkg_arch "$@"
            fi
            ;;
        "version")
            [ -n "$1" ] || fatal "Arg is missed"
            if [ -n "$FNFLAG" ] ; then
                print_version "$(print_pkgname "$@")"
            elif [ -n "$PKFLAG" ] ; then
                print_pkg_version "$@"
            else
                print_version "$@"
            fi
            ;;
        "release")
            [ -n "$1" ] || fatal "Arg is missed"
            if [ -n "$FNFLAG" ] ; then
                print_release "$(print_pkgname "$@")"
            elif [ -n "$PKFLAG" ] ; then
                print_pkg_release "$@"
            else
                print_release "$@"
            fi
            ;;
        "shortname")
            [ -n "$1" ] || exit 0 #fatal "Arg is missed"
            print_shortname "$@"
            ;;
        "version-release")
            [ -n "$1" ] || fatal "Arg is missed"
            if [ -n "$FNFLAG" ] ; then
                print_version_release "$(print_pkgname "$@")"
            elif [ -n "$PKFLAG" ] ; then
                print_pkg_version_release "$@"
            else
                print_version_release "$@"
            fi
            ;;
        "field")
            [ -n "$1" ] || fatal "Arg is missed"
            local FIELD="$1"
            shift
            [ "$1" = "for" ] && shift
            [ "$1" = "package" ] && shift
            query_package_field "$FIELD" "$@"
            ;;
        "pkgname")
            [ -n "$FNFLAG" ] || fatal "print $WHAT works only for filename(s)"
            [ -n "$1" ] || fatal "Arg is missed"
            # TODO: drop_pkg_extensions
            print_pkgname "$@"
            ;;
        "srcname")
            [ -n "$FNFLAG" ] || fatal "print $WHAT works only for filename(s)"
            [ -n "$1" ] || fatal "Arg is missed"
            print_srcname "$@"
            ;;
        "srcpkgname")
            [ -n "$FNFLAG" ] || [ -n "$PKFLAG" ] || fatal "print $WHAT works only for filename(s)"
            [ -n "$1" ] || fatal "Arg is missed"
            print_srcpkgname "$@"
            ;;
        "specname")
            [ -n "$FNFLAG" ] || [ -n "$PKFLAG" ] || fatal "print $WHAT works only for filename(s)"
            [ -n "$1" ] || fatal "Arg is missed"
            print_specname "$@"
            ;;
        "binpkgfilelist")
            # TODO: rpm only
            # TODO: replace get_binpkg_list
            local DIR="$1"
            shift
            [ "$1" = "for" ] && shift
            [ -n "$DIR" ] || fatal "DIR arg is missed"
            [ -n "$1" ] || fatal "source package filename is missed"
            print_binpkgfilelist "$DIR" "$1"
            ;;
        "compare")
            [ "$1" = "version" ] && shift
            [ -n "$1" ] || fatal "Arg is missed"
            #if [ -n "$PKFLAG" ] ; then
            #    query_package_field "name" "$@"
            #else
                 compare_version "$1" "$2"
            #fi
            ;;
        "constructname")
            construct_name "$@"
            ;;
        "info")
            export EPMVERSION
            $DISTRVENDOR "$@"
            ;;
        *)
            fatal "Unknown command $ epm print $WHAT. Use epm print help for get help."
            ;;
    esac
}
